<?php
include ("../../inc/application_settings.php");
include (CATEGORY);
include (ACCOUNT);
include (APPLICATION_COMMON);


/**
 * creates the menue for finacial status
 * @param string item financial status
 * @param string item graphic
 * @return string output
 */
function CreateMenue($costpoint, $p_costpoint, $category, $account) {
	$output = '';

	$output .= '<div id="leftMenue" >';
	$output .= '<ul  id="menueList">';
	$output .= '<li class="itemSelected" destination="' . COSTPOINTS . '" params="T=COSTPOINT">' . $costpoint . '</li>';
	$output .= '<li destination="' . COSTPOINTS . '" params="T=PERMANENT_COSTPOINT">' . $p_costpoint . '</li>';
	$output .= '<li destination="' . COSTPOINTS . '" params="T=CATEGORY">' . $category . '</li>';
	$output .= '<li destination="' . COSTPOINTS . '" params="T=ACCOUNT">' . $account . '</li>';
	$output .= '</ul>';
	$output .= '</div>';
	$output .= '<div id="content" >' . CreateCostpointLayout() . '</div>';

	return $output;
}


/**
 * creates layout for costpoint menue item
 * @return string layout
 */
function CreateCostpointLayout($auth) {
	try {
		//$mysql = $auth->GetSqlConnection();
		//$account = GetLanguageSpecificText($mysql, "konto_menue2", LANGUAGE);

		//$output .= CreateAccountToolbar($mysql);

		$output .= '<form id="form_create_costpoints" action="' . COSTPOINTS . '" name="form_create_costpoints" method="post" style="">';
		$output .= '<table>';
		$output .= '<tr>';
		$output .= '<td><span style="color: #61021B; font-weight:bold">Name:</span></td>';
		$output .= '<td><input type="text" id="costpointName" class="text ui-widget-content ui-corner-all" style="width:200px; height:15px;" /></td>';
		$output .= '<td>Beschreibung:</td>';
		$output .= '</tr>';
		$output .= '<tr>';
		$output .= '<td><span style="color: #61021B; font-weight:bold">Kategorie:</span></td>';
		$output .= '<td><div id="test"></div> <script>$(document).ready(function() {$("#test").dropdown({items:{01:"item1", 02:"<b>item2</b>", 03:"item3"}});});</script></td>';
		$output .= '<td rowspan="5"><textarea id="description" rows="8" class="text ui-widget-content ui-corner-all" style="width:200px;" /></td>';
		$output .= '</tr>';
		$output .= '<tr>';
		$output .= '<td><span style="color: #61021B; font-weight:bold">Parent:</span></td>';
		$output .= '<td><input type="text" id="parentCostpoint" class="text ui-widget-content ui-corner-all" style="width:200px; height:15px;" /></td>';
		$output .= '</tr>';
		$output .= '<tr>';
		$output .= '<td><span style="color: #61021B; font-weight:bold">Konto:</span></td>';
		$output .= '<td><input type="text" id="costpointAccount" class="text ui-widget-content ui-corner-all" style="width:200px; height:15px;" /></td>';
		$output .= '</tr>';
		$output .= '<tr>';
		$output .= '<td><span style="color: #61021B; font-weight:bold">Ort:</span></td>';
		$output .= '<td><input type="text" id="place" class="text ui-widget-content ui-corner-all" style="width:200px; height:15px;" /></td>';
		$output .= '</tr>';
		$output .= '<tr>';
		$output .= '<td><span style="color: #61021B; font-weight:bold">Wert:</span></td>';
		$output .= '<td><input type="text" id="costpointValue" class="text ui-widget-content ui-corner-all" style="width:200px; height:15px;" /></td>';
		$output .= '</tr>';
		$output .= '</table>';
		$output .= '</form>';

		//$output .= CreateAccountTable($auth, $account);

		return $output;
	} catch (Exception $e) {
		return $e->getMessage();
	}
}

/**
 * creates the layout for permanent menue item
 * @return string layout
 */
function CreatePCostpointLayout($auth) {
	return 'permanent costpoint layout';
}






/**
 * creates the default layout
 */
function CreateDefaultLayout($auth) {
	try {
		$mysql = $auth->GetSqlConnection();

		$costpoint = GetLanguageSpecificText($mysql, "costpoint_menue", LANGUAGE);
		$p_costpoint = GetLanguageSpecificText($mysql, "permanentcostpoint_menue", LANGUAGE);
		$category = GetLanguageSpecificText($mysql, "category_menue", LANGUAGE);
		$account = GetLanguageSpecificText($mysql, "konto_menue", LANGUAGE);

		return CreateMenue($costpoint, $p_costpoint, $category, $account);
	} catch (Exception $e) {
		echo $e->getMessage();
	}
}



$menue = (isset($_REQUEST['T'])) ? $_REQUEST['T'] : NULL;
$action = (isset($_REQUEST['A'])) ? $_REQUEST['A'] : NULL;
$categoryId = (isset($_REQUEST['CATEGORY_ID'])) ? $_REQUEST['CATEGORY_ID'] : NULL;
$accountId = (isset($_REQUEST['ACCOUNT_ID'])) ? $_REQUEST['ACCOUNT_ID'] : NULL;
$entity = (isset($_REQUEST['E'])) ? $_REQUEST['E'] : NULL;

try{

	$mysql = new MYSQL(DB_NAME, DB_USER, DB_PASSWORD, DB_SERVER);
	$mysql->Connect();

	$authParams['CONNECTION'] = $mysql;
	$authParams['EMAIL'] = $_COOKIE['EMAIL'];
	$authParams['PASSWORD'] = $_COOKIE['PASSWORD'];
	$auth = new Authentication($authParams);


	if ($auth->CheckAuthentication()){
		//$auth->GetSqlConnection()->SetAuthorization($auth);
		// 		$logText = '<' . $auth->GetUserName() . '> - connect: OK';
		// 		Logger::DebugLog($logText);
		switch ($menue) {
			case 'ACCOUNT':
				switch($entity){
					case 'GET_ACCOUNT':
						GetAccountData($accountId, $auth);
						break;
					default:
						echo CreateAccountLayout($auth);
						break;
				}

				break;
			case 'COSTPOINT' :
				echo CreateCostpointLayout($auth);
				break;
			case 'PERMANENT_COSTPOINT' :
				echo CreatePCostpointLayout($auth);
				break;
			case 'CATEGORY' :
				switch ($entity) {
					case 'GET_CATEGORY' :
						GetCategoryData($categoryId, $auth);
						break;
							
					default :
						echo CreateCategoryLayout($auth);
						break;
				}
				break;

			case 'DML' :
				switch ($entity) {
					case 'CATEGORY' :
						switch ($action) {
							case 'SAVE' :
								SaveCategory($categoryId, $auth);
								break;
									
							case 'DELETE' :
								DeleteCategory($categoryId, $auth);
								break;
						}
						break;

					case 'ACCOUNT':
						switch($action){
							case 'SAVE':
								SaveAccount($accountId, $auth);
								break;

							case 'DELETE':
								DeleteAccount($accountId, $auth);
								break;
						}
						break;
				}
				break;

			case 'AJAX' :
				switch ($entity) {
					case 'CATEGORY_TABLE' :
						echo ReloadCategoryTable($auth);
						break;

					case 'ACCOUNT_TABLE' :
						echo ReloadAccountTable($auth);
						break;
				}
				break;

			default :
				echo CreateDefaultLayout($auth);
				break;
		}
	}

	$mysql->Disconnect();
} catch (Exception $e) {
	echo $e->getMessage();
}
?>



